package com.ict.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.common.core.domain.entity.SysDept;
import com.ict.common.utils.SecurityUtils;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.bean.BeanUtils;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.system.mapper.SysUserRoleMapper;
import com.ict.system.service.ISysDeptService;
import com.ict.web.constant.Constants;
import com.ict.web.constant.ProjectStatusEnum;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.*;
import com.ict.web.dto.PreliminaryInformationAddDto;
import com.ict.web.mapper.PreliminaryInformationMapper;
import com.ict.web.msg.domain.SysMsg;
import com.ict.web.msg.util.CreateMsgUtil;
import com.ict.web.service.IApprovalService;
import com.ict.web.service.IPreliminaryInformationService;
import com.ict.web.service.IProjectManagementService;
import com.ict.web.service.IReviewTaskService;
import com.ict.web.vo.PreliminaryInformationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 初验信息--刘杰Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-01
 */
@Service
public class PreliminaryInformationServiceImpl extends ServiceImpl<PreliminaryInformationMapper,PreliminaryInformation> implements IPreliminaryInformationService
{
    @Autowired
    private PreliminaryInformationMapper preliminaryInformationMapper;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Autowired
    private IYzqFileService fileService;
    @Resource
    private SysUserRoleMapper userRoleMapper;
    /**
     * 查询初验信息--刘杰
     *
     * @param firstCheckId 初验信息--刘杰主键
     * @return 初验信息--刘杰
     */
    @Override
    public PreliminaryInformationVo selectPreliminaryInformationByFirstCheckId(Long firstCheckId)
    {
        PreliminaryInformation preliminaryInformation = preliminaryInformationMapper.selectPreliminaryInformationByFirstCheckId(firstCheckId);
        PreliminaryInformationVo preliminaryInformationVo = new PreliminaryInformationVo();
        BeanUtils.copyProperties(preliminaryInformation,preliminaryInformationVo);
        YzqFile file = fileService.getById(preliminaryInformation.getCertificate());
        if(StringUtils.isNotNull(file)){
            preliminaryInformationVo.setCertificateFile(file);
        }
        return preliminaryInformationVo;
    }

    /**
     * 查询初验信息--刘杰列表
     *
     * @param preliminaryInformation 初验信息--刘杰
     * @return 初验信息--刘杰
     */
    @Override
    public List<PreliminaryInformation> selectPreliminaryInformationList(PreliminaryInformation preliminaryInformation)
    {
        return preliminaryInformationMapper.selectPreliminaryInformationList(preliminaryInformation);
    }

    /**
     * 新增初验信息--刘杰
     *
     * @param dto 初验信息--刘杰
     * @return 结果
     */
    @Override
    public Long insertPreliminaryInformation(PreliminaryInformationAddDto dto)
    {
        PreliminaryInformation bean = PreliminaryInformationAddDto.of(dto);
        //新增初验信息-生成对应系统消息
        SysDept sysDept = deptService.selectDeptById(SecurityUtils.getDeptId());
        ProjectManagement projectManagement = projectManagementService.getById(dto.getProId());
        Assert.isTrue(StringUtils.isNotNull(projectManagement),"未找到该项目信息");
        String content=projectManagement.getNickName()+ CreateMsgUtil.SUBMITTED+projectManagement.getName()+CreateMsgUtil.PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED;
        //大数据局收到消息
        List<Long> bigDataId = userRoleMapper.getUserIdByRoleId(Constants.ROLE_TYPE_BIGDATA);
        List<Long> bigDataNormalId = userRoleMapper.getUserIdByRoleId(Constants.ROLE_TYPE_BIGDATA_NORMAL);
        bigDataId.addAll(bigDataNormalId);
        bigDataId.forEach(s->{
            CreateMsgUtil.createMsg(content,s,null,CreateMsgUtil.PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED_TITLE, SysMsg.RESOURCE_TYPE_DATA,null, null);
        });

        //同步到项目信息
        projectManagement.setFirstCheckId(bean.getFirstCheckId());
        //存储合同扫码件文件
        YzqFile contractFile = dto.getCertificate();
        contractFile.setId(String.valueOf(IdUtils.snowId()));
        contractFile.setDelFlag(ExtractExpert.DELETE_STATUS_NORMAL);
        fileService.save(contractFile);
        bean.setCertificate(contractFile.getId());

        ReviewTask reviewTask = reviewTaskService.getById(projectManagement.getReviewTaskId());
        org.springframework.util.Assert.isTrue(StringUtils.isNotNull(reviewTask),"评审任务不能为空");
        Approval approval = approvalService.getApproval(projectManagement.getProId(), projectManagement.getIsState(),null,null);
        approval.setApprovalNode(ProjectStatusEnum.PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED.get().getNode());
        approval.setApprovalStage(ProjectStatusEnum.PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED.get().getStage());
        approval.setApprovalStatus(ProjectStatusEnum.PRELIMINARY_INSPECTION_AND_FILING_SUBMITTED.get().getStatus());
        approval.setApprovalOperator(SecurityUtils.getNickName());
        approval.setEndTime(LocalDateTime.now());
        projectManagement.setIsState(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getStatus());
        projectManagement.setNode(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getNode());
        projectManagement.setStag(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getStage());
        //新增终验备案进程
        approvalService.save(new Approval(IdUtils.snowId(),projectManagement.getProId(),ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getStage(),
                ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getNode(), LocalDateTime.now(),null, null,ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getStatus(),
                "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
        reviewTask.setNode(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getNode());
        reviewTask.setNodeStatus(ProjectStatusEnum.FINAL_ACCEPTANCE_RECORD_TO_BE_SUBMITTED.get().getStatus());
        projectManagementService.updateById(projectManagement);
        reviewTaskService.updateById(reviewTask);
        approvalService.updateById(approval);
        preliminaryInformationMapper.insert(bean);
        //同步项目数据到项目信息表中
        approvalService.synchronizationProjectInfo(projectManagement);
        return bean.getFirstCheckId();
    }

    /**
     * 修改初验信息--刘杰
     *
     * @param firstCheckId 初验信息--刘杰
     * @return 结果
     */
    @Override
    public int updatePreliminaryInformation(PreliminaryInformationAddDto dto,Long firstCheckId)
    {
        PreliminaryInformation bean = preliminaryInformationMapper.selectById(firstCheckId);
        BeanUtils.copyProperties(dto,bean);
        bean.setUpdateTime(LocalDateTime.now());
        YzqFile file = fileService.getById(bean.getCertificate());
        BeanUtils.copyProperties(dto.getCertificate(),file);
        fileService.updateById(file);
        return preliminaryInformationMapper.updateById(bean);
    }

    /**
     * 删除初验信息--刘杰信息
     *
     * @param firstCheckId 初验信息--刘杰主键
     * @return 结果
     */
    @Override
    public int deletePreliminaryInformationByFirstCheckId(Long firstCheckId)
    {
        PreliminaryInformation bean = preliminaryInformationMapper.selectById(firstCheckId);
        bean.setDelFlag("1");
        return preliminaryInformationMapper.updateById(bean);
    }
}
